C++ provides "constructor" and "destructor" member functions which are automatic- ally invoked upon allocation and deallocation of objects. Typically the constructor initializes the values of the instance variables and/or performs dynamic memory allocation, while the destructor deallocates any dynamically-allocated memory.
This section extends the Generic_Class files given in the example in Chapter 4 and suggests new global functions new_init() and destroy_delete() to supplant the TC new() and delete() functions defined in the oops library. These alternative functions automatically invoke an object's init() and destroy() member functions to simulate C++ constructors and destructors. They also work for both direct and indirect objects*.
Unlike the C++ constructor/destructor approach, however, this technique has the potential to ensure that any dynamic memory allocation in the init() method succeeds before an object pointer is assigned. That is, new_init() returns either: (1) NULL if the object cannot be allocated, (2) NULL if the object is allocated but its init() method fails (in which case all allocated memory is released), or (3) a pointer to a successfully-initialized object. IMPORTANT: the code on the following pages must be modified to work in C++, as discussed in the comments.